// 本文件为title组件相关API

import axios from "@/js/my_axios"

function get_filter_field(obj, filters = undefined) {
    for (let key in filters) {
        if (key === 'case_name') {
            obj.case_name_field_filter = filters
        } else if (key === 'status') {
            obj.status_field_filter = filters
        } else if (key === 'execute_user') {
            obj.user_field_filter = filters
        } else if (key === 'environment') {
            obj.environment_field_filter = filters
        }
    }
    let filter_list = []
    if (obj.time_filter) {
        filter_list.push(obj.time_filter)
    }
    if (obj.case_name_field_filter) {
        filter_list.push(obj.case_name_field_filter)
    }
    if (obj.status_field_filter) {
        filter_list.push(obj.status_field_filter)
    }
    if (obj.user_field_filter) {
        filter_list.push(obj.user_field_filter)
    }
    if (obj.environment_field_filter) {
        filter_list.push(obj.environment_field_filter)
    }
    return filter_list
}


//基础测试创建任务
function add_test(test_info) {
    console.log(test_info)
    if (!test_info) {
        return
    }
    return axios({
        method: 'post',
        url: 'test_info/',
        data: test_info,
    }).then((response) => {
        console.log(response.data)
        return response
    }).catch((error) => {
        console.log(error);
    });
}

//获取测试详情
function get_test_info(page, page_size) {
    return axios({
        method: 'get',
        url: 'test_info/',
        params: {
            'page': page,
            'page_size': page_size,
        },
    }).then((response) => {
        return response
    }).catch((error) => {
        console.log(error);
    });
}

// 删除
function delete_test_info(val) {
    var status = false
    var val_list = []
    if (Array.isArray(val)) {
        val_list = JSON.parse(JSON.stringify(val))
        status = true
    }
    return axios({
        method: 'delete',
        url: 'test_info/',
        data: {
            info: status ? val_list : val,
        },
    }).then((response) => {
        return response
    }).catch((error) => {
        console.log(error);
    });
}

// 以表头为筛选条件
function filter_handler(filter) {
    return axios({
        method: 'get',
        url: 'query_data/',
        params: filter,
    }).then((response) => {
        return response
    }).catch((error) => {
        console.log(error);
    });
}

// 过滤查询
function filter_query(page, page_size, filter) {
    console.log(page, page_size, filter)
    return axios({
        method: 'post',
        url: 'query_data/',
        params: {
            'page': page,
            'page_size': page_size,
        },
        data: {
            'filter': filter ? filter : '',
        },
    }).then((response) => {
        return response
    }).catch((error) => {
        console.log(error);
    });
}

// 日志详情
function read_log(val) {
    let info = {
        file_path: val,
    }
    return axios({
        method: 'post',
        url: 'read_log/',
        data: info,
    }).then((response) => {
        return response
    }).catch((error) => {
        console.log(error);
    });
}

function start_task(val) {
    // 启动
    let data = {
        data: val,
        method: 'start'
    }
    return axios({
        method: 'post',
        url: 'run_base_case/',
        data: data
    }).then((response) => {
        return response
    }).catch((error) => {
        console.log(error);
    });
}

function stop_or_continue_task(val) {
    // 停止或继续
    let info = {
        data: val,
    }
    if (val.status === 'stop'){
        info['method'] = 'continue'
    }else{
        info['method'] = 'stop'
    }
    return axios({
        method: 'post',
        url: 'run_base_case/',
        data: info
    }).then((response) => {
        return response
    }).catch((error) => {
        console.log(error);
    });
}

function kill_task(val) {
    // 停止或继续
    let info = {
        data: val,
        method: 'kill'
    }
    return axios({
        method: 'post',
        url: 'run_base_case/',
        data: info
    }).then((response) => {
        return response
    }).catch((error) => {
        console.log(error);
    });
}

function get_police(val) {
    // 获取报警机器人列表
    return axios({
        method: 'get',
        url: 'police/',
    }).then((response) => {
        return response
    }).catch((error) => {
        console.log(error);
    });
}

//修改任务
function modify_test(test_info) {
    if (!test_info) {
        return
    }
    return axios({
        method: 'put',
        url: 'test_info/',
        data: test_info,
    }).then((response) => {
        return response
    }).catch((error) => {
        console.log(error);
    });
}


export {
    add_test,
    get_test_info,
    delete_test_info,
    filter_handler,
    get_filter_field,
    filter_query,
    read_log,
    start_task,
    stop_or_continue_task,
    kill_task,
    modify_test,
    get_police,
}